layui.use(["element", 'layer', 'form', 'table'], function () {
  var element = layui.element,
    form = layui.form,
    layer = layui.layer,
    table = layui.table;
  form.on('select(AssetClass)', function (data) {
    vm.AssetClass = data.value;
  });
  setTable(vm.tableIns);
  // 重新渲染表格
  function setTable(data) {
    table.render({
      elem: '#table1',
      data: data,
      limit:50,
      cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
      cols: [
        [ //标题栏
          {
            field: 'id',
            title: 'ID',
            width: 50
          }, {
            field: 'AssetName',
            title: '资产名称',
            minWidth: 120
          }, {
            field: 'AssetClass',
            title: '资产类别(大类)',
            minWidth: 150
          }, {
            field: 'CompanyName',
            title: '企业名称',
            minWidth: 160
          }, {
            field: 'amount',
            title: '配置金额(万元)',
            width: 140
          }, {
            field: 'proportion',
            title: '配置比例',
            width: 100
          }, {
            field: 'UpdateTime',
            title: '更新时间',
            width: 120
          }, {
            fixed: 'right',
            title: '操作',
            toolbar: '#barDemo',
            width: 60
          }
        ]
      ]
    });
  }
  $('#searchBtn').on('click', function () {
    var AssetClass = $('#AssetClass').val();
    var CompanyName = vm.CompanyName;
    var Arr = [];
    if (AssetClass == 0) {
      if (CompanyName) {
        Arr = vm.defaultData.filter(function (item) {
          return item.CompanyName == CompanyName
        });
        vm.total(Arr);
      }
      else {
        Arr = vm.defaultData;
        vm.totalVisible = false;
      }
      setTable(Arr);
    }
    else {
      if (CompanyName) {
        var Da = vm.defaultData.filter(function (item) {
          return item.AssetClass == AssetClass
        });
        Arr = Da.filter(function (item) {
          return item.CompanyName == CompanyName
        });
      }
      else {
        Arr = vm.defaultData.filter(function (item) {
          return item.AssetClass == AssetClass
        });
      }
      setTable(Arr);
      vm.total(Arr);
    }
  });
  //监听行工具事件
  table.on('tool(demo)', function (obj) {
    var data = obj.data;
    if (obj.event === 'detail') {
      if (data.id < 6) {
        parent.location.href = '../../main.html?url=html/DLZCPZMX/CCXQ_1.html&id=25&txt=产品资产持仓详情';
      }
      else {
        parent.location.href = '../../main.html?url=html/DLZCPZMX/CCXQ_2.html&id=25&txt=产品资产持仓详情';
      }
    }
  });

});
// AssetCode  资产代码  

var vm = new Vue({
  el: '#app',
  data: {
    AssetClass: '0',
    CompanyName: '',
    AssetName: '',
    ArrData: [],
    status:localStorage.getItem('status')?localStorage.getItem('status'):0,
    totalVisible: false,
    tableIns: [{
      "id": "1",
      "AssetName": "非标资产A1",
      "AssetClass": "非标准化债权类资产",
      "CompanyName": "企业A",
      "amount": "100,000",
      "proportion": "20%",
      "UpdateTime": "2018-10-11"
    }, {
      "id": "2",
      "AssetName": "债券资产A1",
      "AssetClass": "债券",
      "CompanyName": "企业A",
      "amount": "200,000",
      "proportion": "40%",
      "UpdateTime": "2018-10-12"
    }, {
      "id": "3",
      "AssetName": "同业存款资产A1",
      "AssetClass": "同业存款",
      "CompanyName": "同业A",
      "amount": "150,000",
      "proportion": "30%",
      "UpdateTime": "2018-10-12"
    }, {
      "id": "4",
      "AssetName": "同业存单资产A1",
      "AssetClass": "同业存单",
      "CompanyName": "同业A",
      "amount": "40,000",
      "proportion": "8%",
      "UpdateTime": "2018-10-13"
    }, {
      "id": "5",
      "AssetName": "回购资产C1",
      "AssetClass": "回购",
      "CompanyName": "同业C",
      "amount": "10,000",
      "proportion": "2%",
      "UpdateTime": "2018-10-13"
    }, {
      "id": "6",
      "AssetName": "非标资产B1",
      "AssetClass": "非标准化债权类资产",
      "CompanyName": "企业B",
      "amount": "250,000",
      "proportion": "36%",
      "UpdateTime": "2018-10-13"
    }, {
      "id": "7",
      "AssetName": "债券资产B1",
      "AssetClass": "债券",
      "CompanyName": "企业B",
      "amount": "100,000",
      "proportion": "14%",
      "UpdateTime": "2018-10-13"
    }, {
      "id": "8",
      "AssetName": "同业存款资产B1",
      "AssetClass": "同业存款",
      "CompanyName": "同业B",
      "amount": "200,000",
      "proportion": "29%",
      "UpdateTime": "2018-10-13"
    }, {
      "id": "9",
      "AssetName": "同业存单资产B1",
      "AssetClass": "同业存单",
      "CompanyName": "同业B",
      "amount": "70,000",
      "proportion": "10%",
      "UpdateTime": "2018-10-13"
    }, {
      "id": "10",
      "AssetName": "回购资产C2",
      "AssetClass": "回购",
      "CompanyName": "同业C",
      "amount": "80,000",
      "proportion": "11%",
      "UpdateTime": "2018-10-13"
    }],
    defaultData: [],
    px_txt: ''
  },
  mounted: function () {
    this.init()
  },
  methods: {
    init: function () {
      localStorage.setItem('navigation', '<a href="#">首页</a><span lay-separator="">/</span><a href="">投资集中度管理</a><span lay-separator="">/</span><a><cite>资产集中度</cite></a>');
      if (this.status == 1) {
        // this.tableIns =[];
        var arry = ["10;回购资产C2;回购;同业C;80,000;11%;2018-10-13", "11;非标资产A2;非标准化债权类资产;企业A;100,000;10%;2018-12-10", "12;债券资产A2;债券;企业A;200,000;20%;2018-12-10", "13;同业存款资产C1;同业存款;同业C;400,000;40%;2018-12-10", "14;同业存单资产D1;同业存单;同业D;100,000;10%;2018-12-10", "15;回购资产D1;回购;同业D;50,000;5%;2018-12-10"];
        var keys = ['id', 'AssetName', 'AssetClass', 'CompanyName', 'amount', 'proportion', 'UpdateTime'];
        arry.map(item => {
          var keyData = item.split(";");
          var obj = {};
          keyData.map((item, index) => {
            obj[keys[index]] = item;
          });
          this.tableIns.push(obj);
        });
      }
      this.defaultData = this.tableIns;
    },
    total: function (data) {
      var tableData = data;
      this.totalVisible = data.length == 0 ? false : true;
      var total_lc = this.status == 0?1200000:2200000;
      this.px_txt = "";
      var name = _.uniq(data.map(item=>{return item.CompanyName}));
      var ar = [];
      name.map((item,index)=>{
        var nstr=[];
        data.map((e, n) => {
          if(e.CompanyName == item){ 
            nstr.push(n)
          }
        });
        ar.push([item,nstr]);
      });
      ar.map((item, index) => { 
        var am=0,pro=0;
        item[1].map(im => { 
          am +=Number(data[im].amount.replace(/,/g, ''));
        });
        pro = (am / total_lc)*100;
        this.px_txt += '<p>' + item[0] + data[item[1][0]].AssetClass + '配置金额：' + am.toLocaleString() + '万元，占比：' + pro.toFixed(2) + '%</p>';
      })
    }
  }
})